package com.mobily.teamaster.reports.client.screen;

import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.TitleOrientation;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.DateItem;
import com.smartgwt.client.widgets.layout.VLayout;

public class RatingReportScreen extends Composite {
	
	VLayout layout;
	VerticalPanel mainPanel;

	public RatingReportScreen() {
		init();
	}

	public void init(){
		mainPanel = new VerticalPanel();
//		criteriaVPanel.add()
		 layout = new VLayout(8); 
		 layout.setLeft(15);
		 initWidget(layout);
		 
		 DynamicForm form = new DynamicForm();  
	        form.setMaxWidth(400);  
//	        form.setHeight(22);  
	        form.setTitleOrientation(TitleOrientation.TOP);  
	        

	  
	        final DateItem ratingFromDate = new DateItem ("From");
	        final DateItem ratingToDate = new DateItem ("To");
//	        DateRange dateRange = new DateRange();  
//	        dateRange.setRelativeStartDate(new RelativeDate("-1200m"));  
//	        dateRange.setRelativeEndDate(new RelativeDate("-1000m"));  
//	        createdRangeItem.setValue(dateRange);  
	  
//	        form.setItems(createdRangeItem);
	        
//	        final ComboBoxItem activityCombo = new ComboBoxItem();  
//	        activityCombo.setTitle("Activity");  
////	        activityCombo.setHint("<nobr>Select Activity</nobr>");  
//	        activityCombo.setType("comboBox");  
//	        activityCombo.setValueMap("All", "onTime", "clean", "smile", "lookgood", "like", "Espresso", "Capuchino", "Tea", "Choclate"); 
//	        activityCombo.setValue("All");
//	        
//	        final ComboBoxItem userTypeCombo = new ComboBoxItem();  
//	        userTypeCombo.setTitle("User Type");  
////	        userTypeCombo.setHint("<nobr>Select User Type</nobr>");  
//	        userTypeCombo.setType("comboBox");  
//	        userTypeCombo.setValueMap("All", "STF", "VND", "MGR"); 
//	        userTypeCombo.setValue("All");
	        
//	        final SelectItem groupBySelect = new SelectItem();  
//	        groupBySelect.setTitle("Group By");  
//	        groupBySelect.setMultiple(false);  
//	        groupBySelect.setMultipleAppearance(MultipleAppearance.GRID);  
//	        LinkedHashMap<String, String> groupByColumnMap = new LinkedHashMap<String, String>();
//	        groupByColumnMap.clear();
//	        groupByColumnMap.put("col3", "Date");
//	        groupByColumnMap.put("col2", "Tea Boy");
//	        groupByColumnMap.put("col0", "Activity");
//	        groupBySelect.setValueMap(groupByColumnMap);  
	  
//	        final ComboBoxItem groupBySelect = new ComboBoxItem();  
//	        groupBySelect.setTitle("Group By");  
//	        LinkedHashMap<String, String> groupByColumnMap = new LinkedHashMap<String, String>();
//	        groupByColumnMap.clear();
//	        groupByColumnMap.put("col3", "Date");
//	        groupByColumnMap.put("col2", "Tea Boy");
//	        groupByColumnMap.put("col0", "Activity");
//	        groupBySelect.setValueMap(groupByColumnMap);  
//	        groupBySelect.setValue("col2");
	        

	        layout.addMember(new Label("<b><nobr>Rating Wise Report</nobr></b>"));
	        layout.addMember(form);  
	        IButton searchButton = new IButton("Show");  
	        searchButton.setAutoFit(true);  
	        searchButton.setAlign(Alignment.CENTER);
	        layout.addMember(searchButton);  
	        searchButton.addClickHandler(new ClickHandler() {  
	            @Override  
	            public void onClick(ClickEvent event) {  
//	                Criterion criteria = createdRangeItem.getCriterion(); 
//	                System.out.println("cri Map : "+criteria.getValues());
//	                System.out.println("Value : "+createdRangeItem.getAttributes());
//	                System.out.println("from : "+ratingFromDate.getValue());
	            	 String fromdt = Util.getDate(ratingFromDate.getDisplayValue())+" 00:00:00";
		             String todt = Util.getDate(ratingToDate.getDisplayValue())+" 23:59:59";
		               
	                System.out.println("from : "+fromdt);
	                System.out.println("to : "+todt);
	                generateRatingReport(fromdt,todt);
	            }  
	        });  
//	        mainPanel.setWidth("600px");
//	        mainPanel.setHeight("600px");
	        layout.addMember(mainPanel);
	        
//	        form.setItems(reportType, createdRangeItem, activityCombo, userTypeCombo, groupBySelect);
//	        form.setItems(reportType);
//	        form.setItems( createdRangeItem);
//	        form.setItems(activityCombo, userTypeCombo, groupBySelect);
//	        form.draw();
	        
	        form.setFields(ratingFromDate, ratingToDate);
	        
	        layout.redraw();
//		RootPanel.get("reportCriteriaContainer").add(layout);
	}
	
	private void generateRatingReport(String fromDate, String toDate) {
		// fromDate = "6%2F6%2F11+6%3A30+AM";
		// toDate = "6%2F2%2F11+6%3A30+AM";
		fromDate = URL.encode(fromDate);
		toDate = URL.encode(toDate);
		
		System.out.println("fromDate : " + fromDate);
		System.out.println("toDate : " + toDate);
		
		String query = "select+col1%2C+SUM(col2) as Total+from+930600+where+col3+%3E%3D+'"+fromDate+"'+and+col3+%3C%3D+'"+toDate+"'&qrs=+and+col1+%3E%3D+&qre=+and+col1+%3C%3D+&qe=+group+by++col1";
		System.out.println("query : " + query);
		// HTML chart = new
		// HTML("<iframe width=\"500px\" height=\"300px\" scrolling=\"no\" frameborder=\"no\" src=\"http://www.google.com/fusiontables/embedviz?gco_chartArea=%7B%22top%22%3A%2230%22%7D&containerId=gviz_canvas&q=select+col1%2C+AVERAGE(col2)+from+930600+where+col3+%3C%3D+'6%2F6%2F11+6%3A30+AM'+and+col3+%3E%3D+'6%2F2%2F11+6%3A30+AM'&qrs=+and+col1+%3E%3D+&qre=+and+col1+%3C%3D+&qe=+group+by++col1+limit+2&viz=GVIZ&t=BAR&width=500&height=300\"></iframe>");
		HTML chart = new HTML(
				"<iframe width=\"500px\" height=\"300px\" scrolling=\"no\" frameborder=\"no\" src=\"http://www.google.com/fusiontables/embedviz?gco_chartArea=%7B%22top%22%3A%2230%22%7D&containerId=gviz_canvas&q=" +
				query + "&viz=GVIZ&t=BAR&width=500&height=300\"></iframe>");
		// RootPanel.get("reportContainer").add(chart);
		// RootPanel.get().add(chart);
		if (mainPanel.getWidgetCount() > 0)
			mainPanel.remove(0);
		mainPanel.add(chart);
		// layout.redraw();
	}

}
